package com.qk.controller;

import com.qk.PageResult;
import com.qk.common.Result;
import com.qk.entity.Dept;
import com.qk.service.DeptService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
public class DeptController {
    @Autowired
    private DeptService deptService;
    @PostMapping("/depts")
    public Result add(@RequestBody Dept dept){
        log.info("添加部门",dept);
        System.out.println(dept);
        deptService.add(dept);
        return Result.success();

    }

    @GetMapping("/depts")
    public Result list(String name, Integer status,@RequestParam(defaultValue = "1") Integer page,@RequestParam(defaultValue = "5") Integer pageSize){
        System.out.println( name+ "--"+status+ "--"+page+"--"+pageSize);
        log.info("查询部门",name,status,page,pageSize);
        PageResult<Dept> pageResult = deptService.listDepts(name, status, page, pageSize);
        return Result.success(pageResult);
    }

   @GetMapping("/depts/{id}")
    public Result getDeptById(@PathVariable Integer id){

        log.info("查询部门",id);
        Dept dept = deptService.getDeptById(id);
        return Result.success(dept);
    }

    @PutMapping("/depts")
    public Result update(@RequestBody Dept dept){
        log.info("更新部门");
        log.info("部门",dept);
        deptService.update(dept);
        System.out.println(dept);
        return Result.success();
    }

    @DeleteMapping("/depts/{id}")
    public Result deleteById(@PathVariable Integer id){
        log.info("删除部门",id);
        deptService.deleteById(id);
        return Result.success();
    }

    // 查询所有的部门
    @GetMapping("/depts/list")
    public Result listDepts(){
        log.info("查询所有部门");
        List<Dept> depts = deptService.listAllDepts();
        return Result.success(depts);

    }


}